// pages/collect/index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nav: [{ name: '直播' }, { name: '录播' }, { name: '面授' }, { name: '试听' }],
    currentId: 0,
    list: [],
    page: 1,
    pagesize: '',
    endloading: false,
    type: 1,
    searchValue: ""
  },
  startSearch(e) {
    this.setData({
      searchValue: e.detail.value,
      page: 1
    })
    wx.showLoading({
      title: '正在搜索...',
    })
    setTimeout(() => {
      wx.hideLoading()
      this.getList()
    }, 500)
  },
  current(e) {
    this.setData({
      currentId: e.currentTarget.id,
    })
    if (this.data.currentId == 1) {
      this.setData({
        page: 1,
        type: 2
      })
      this.getList()
    } else if (this.data.currentId == 2) {
      this.setData({
        page: 1,
        type: 3
      })
      this.getList()
    } else if (this.data.currentId == 3) {
      this.setData({
        page: 1,
        type: 4
      })
      this.getList()
    } else {
      this.setData({
        page: 1,
        type: 1
      })
      this.getList()
    }
  },
  liveDetails(e) {
    let id = e.currentTarget.dataset.id
    let idx = e.currentTarget.id
    let pinAllow = this.data.list[idx].collage_allow
    if (this.data.currentId == 0 && pinAllow == 1) {
      wx.navigateTo({
        url: '/pages/livePt/index?id=' + id,
      })
    } else if (this.data.currentId == 0 && pinAllow == 0) {
      wx.navigateTo({
        url: '/pages/liveDetails/index?id=' + id,
      })
    } else if (this.data.currentId == 1 && pinAllow == 1) {
      wx.navigateTo({
        url: '/pages/recPt/index?id=' + id
      })
    } else if (this.data.currentId == 1 && pinAllow == 0) {
      wx.navigateTo({
        url: '/pages/recDetails/index?id=' + id
      })
    } else if (this.data.currentId == 2 && pinAllow == 1) {
      wx.navigateTo({
        url: '/pages/facePt/index?id=' + id
      })
    } else if (this.data.currentId == 2 && pinAllow == 0) {
      wx.navigateTo({
        url: '/pages/faceDetails/index?id=' + id
      })
    } else {
      wx.navigateTo({
        url: '/pages/stDetails/index?id=' + id
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getList()
  },
  getList() {
    let data = {
      method: 'user.collectlist',
      token: wx.getStorageSync('token'),
      mtype: 1,
      page: this.data.page,
      type: this.data.type,
      keywords: this.data.searchValue
    }
    app.api.post('/api/index.html', data).then((res) => {
      if (res.status) {
        this.setData({
          pagesize: res.data.last_page
        })
        var datas = this.data.list
        if (this.data.page == 1) {
          datas = []
        }
        if (res.data.list.lenght == 0) {
          this.setData({
            endloading: true, //上拉不在加载
          })
        } else {
          this.setData({
            page: this.data.page + 1 //更新page 请求下一页数据
          })
          this.setData({
            list: datas.concat(res.data.list),
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    if (this.data.page <= this.data.pagesize) {
      wx.showLoading({
        title: '加载中'
      })
      setTimeout(() => {
        wx.hideLoading({})
        that.getList()  //页面上拉调用这个方法
      }, 1000)
    } else {
      wx.showToast({
        title: '我已经到底了',
        icon: 'none'
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})